﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LIUS.Beta.FileSystem.PathFileSuffix;

namespace Kopain2
{

    /// <summary>This class is a Document as Kopain sees it.  Several documents are collected in a DocumentTree or Concept.
    /// KopainDocument is close to a visual thing.  Closer to the database/business logic/hard drive it is called Concept.
    /// </summary>
    public class KopainDocument : System.ComponentModel.INotifyPropertyChanged
    {

#region	Private properties.

        private KopainDocumentTree _documentList;
        private Guid _id;
        private FilenameSuffix _filenameSuffix;
        private string _name;
        private KopainDocument _parent;
        private Path _path;
        private string _type;

        /// <summary>This property is set to true if the Document is a folder-document and the document is expanded to show its children.
        /// </summary>
        private bool _childrenAreVisible;

#endregion	//	Private properties.

#region	Public properties.

        public KopainDocumentTree Children
        {
            get
            {
                return _documentList;
            }
            set
            {
                _documentList = value;
            }
        }

        public Guid ID { get { return _id; } }

        public string Name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value; OnPropertyChanged("Name");
            }
        }

        public string Type
        {
            get { return _type; }
        }

        public bool Expanded { get { return _childrenAreVisible; } set { _childrenAreVisible = value; } }

#endregion	//	Public properties.

#region	Constructors.

        /// <summary>Private constructor that takes all mandatory data.
        /// </summary>
        /// <param name="id"></param>
        /// <param name="parent"></param>
        /// <param name="name"></param>
        /// <param name="type"></param>
        /// <param name="path"></param>
        /// <param name="filenameSuffix"></param>
        /// <param name="childrenAreVisible"></param>
        private KopainDocument(Guid id, KopainDocument parent, string name, string type, Path path, FilenameSuffix filenameSuffix, bool childrenAreVisible)
        {
            _id = id;
            _parent = parent;
            _name = name;
            _type = type;

            _path = path;
           _filenameSuffix = filenameSuffix;
            _childrenAreVisible = childrenAreVisible;
        }

        /// <summary>This constructor creates a directory document.
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="id"></param>
        /// <param name="name"></param>
        /// <param name="path"></param>
        /// <param name="childrenAreVisible"></param>
        /// <returns></returns>
        internal static KopainDocument CreateDirectory(KopainDocument parent, Guid id, string name, Path path, bool childrenAreVisible)
        {
            return new KopainDocument(id, parent, name, "dir", path, null, childrenAreVisible);
        }

        /// <summary>This constructor creates a file document.
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="id"></param>
        /// <param name="name"></param>
        /// <param name="filenameSuffix"></param>
        /// <returns></returns>
        internal static KopainDocument CreateFile(KopainDocument parent, Guid id, string name, FilenameSuffix filenameSuffix)
        {
            return new KopainDocument(id, parent, name, "txt", null, filenameSuffix, false);
        }

#endregion	//	Constructors.

#region	Private methods

        private void OnPropertyChanged(string name)
        {
            System.ComponentModel.PropertyChangedEventHandler temp = PropertyChanged;
            if (temp != null)
            {
                temp(this, new System.ComponentModel.PropertyChangedEventArgs(name));
            }
        }

#endregion	//	Private methods

#region INotifyPropertyChanged Members

        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

#endregion
    }

    /// <summary>This class represents the document/note tree which makes a Concept.
    /// </summary>
    public class KopainDocumentTree : System.Collections.ObjectModel.ObservableCollection<KopainDocument>
    {

        /// <summary>This property is all children one step down in the tree.
        /// </summary>
        private KopainDocumentTree _childrenDocumentList;

        /// <summary>This getter returns all children of this leaf.  It allocates the underlying list automagically.
        /// </summary>
        public KopainDocumentTree Children
        {
            get
            {
                if (null == _childrenDocumentList)
                {
                    _childrenDocumentList = new KopainDocumentTree();
                }
                return _childrenDocumentList;
            }
        }

        /// <summary>Default contructor.
        /// </summary>
        public KopainDocumentTree()
        {
        }

        internal void Populate(KopainConcept.Concept concept)
        {
            this.Clear();
            foreach (KopainConcept.Concept.Note note in concept.NoteList)
            {
                this.Add(KopainDocument.CreateFile(null, note.ID, note.Title, note.FilenameSuffix));
            }
        }

        internal void ReplaceWith(KopainDocumentTree documentList)
        {
            this.Clear();
            foreach (KopainDocument node in documentList)
            {
                this.Add(node);
            }
        }

    }

}
